﻿@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using TaskoMask.BuildingBlocks.Contracts.Dtos.Owners
@inject Microsoft.Extensions.Configuration.IConfiguration Configuration
@inject OwnerApiService OwnerApiService
@inject NavigationManager Navigation
@inject SignOutSessionStateManager SignOutManager
@implements IDisposable

<nav style="justify-content:normal !important; display:block; z-index:99;" class="navbar navbar-dark bg-dark fixed-top">
    <div class="float-left">
        <a class="navbar-brand" href="@websiteUrl">TaskoMask</a>
    </div>
    <AuthorizeView>

        <Authorized>
            <div class="float-left">
                <a class="m-md-1 m-lg-1 btn btn-sm btn-primary" href="/">
                    <i class="bi bi-speedometer" title="dashboard"></i>
                </a>
            </div>
            <div class="float-left">
                <div class="dropdown show">
                    <a class="m-md-1 m-lg-1 btn btn-sm btn-success dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <i class="bi bi bi-plus-circle"></i>
                    </a>

                    <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
                        <a class="dropdown-item" @onclick="@(() => ModalService.Show<Pages.Organizations.Components.AddOrganization>("Add Workspace", new ModalOptions { Class="blazored-modal w-75 w-md-50 bg-light-gray"}))">
                            <i class="bi bi-building"></i>
                            Add Workspace
                        </a>

                        <a class="dropdown-item" @onclick="@(() => ModalService.Show<Pages.Projects.Components.AddProject>("Add Project", new ModalOptions { Class="blazored-modal w-75 w-md-50 bg-light-gray"}))">
                            <i class="bi bi-kanban"></i>
                            Add Project
                        </a>

                        <a class="dropdown-item" @onclick="@(() => ModalService.Show<Pages.Boards.Components.AddBoard>("Add Board", new ModalOptions { Class="blazored-modal w-75 w-md-50 bg-light-gray"}))">
                            <i class="bi bi-clipboard-plus"></i>
                            Add Board
                        </a>
                    </div>
                </div>
            </div>

            <div class="float-right">
                <a class="m-md-1 m-lg-1 btn btn-sm btn-warning" id="update_user_profile" @onclick="@(() => ModalService.Show<Pages.Owner.Components.UpdateOwner>("Update Profile", new ModalOptions { Class="blazored-modal w-75 w-md-50 bg-light-gray"}))">
                    <i class="bi bi-person-circle"></i>
                    @(userDisplayName.Length < 6 ? userDisplayName : userDisplayName.Substring(0, 5) + "...")
                </a>
                <a class="m-md-1 m-lg-1 btn btn-sm btn-danger" @onclick="BeginSignOut">
                    <i class="bi bi-power"></i>
                </a>

            </div>
        </Authorized>
        <NotAuthorized>
            <div class="float-right">
                <a class="m-md-1 m-lg-1 btn btn-sm btn-success" href="authentication/login">
                    login
                </a>
                <a class="m-md-1 m-lg-1 btn btn-sm btn-primary" href="register">
                    register
                </a>
            </div>
        </NotAuthorized>
    </AuthorizeView>
</nav>

@code {

    string websiteUrl = "#";
    string userDisplayName = "";


    /// <summary>
    ///
    /// </summary>
    protected override async Task OnInitializedAsync()
    {
        //subscribe to OnMessage event. It is used when another components need to send a message to this component
        ComponentMessageService.OnMessage += ReloadUserDisplayNameHandler;

        websiteUrl = Configuration["Url:Website"];

        await GetAndSetUserDisplayName();



        await base.OnInitializedAsync();
    }



    /// <summary>
    ///
    /// </summary>
    private async Task BeginSignOut(MouseEventArgs args)
    {
        await SignOutManager.SetSignOutState();
        Navigation.NavigateTo("authentication/logout");
    }



    /// <summary>
    ///This is a handler to be notified when user display name is changed somewhere
    /// </summary>
    private async void ReloadUserDisplayNameHandler(MessageType messageType)
    {
        try
        {
            if (ReloadDataHelper.NavMenu_Need_Reload(messageType))
            {
                var ownerResult = await OwnerApiService.GetAsync();
                if (ownerResult.IsSuccess)
                {
                    await GetAndSetUserDisplayName();
                    StateHasChanged();
                }
            }
        }
        catch
        {
            //Async task failure because of async void challenges
        }
    }



    /// <summary>
    ///
    /// </summary>
    private async Task GetAndSetUserDisplayName()
    {
        var ownerResult = await OwnerApiService.GetAsync();
        if (ownerResult.IsSuccess)
            userDisplayName = ownerResult.Value.DisplayName;
    }



    /// <summary>
    ///
    /// </summary>
    public void Dispose()
    {
        // unsubscribe from OnMessage event
        ComponentMessageService.OnMessage -= ReloadUserDisplayNameHandler;
    }
}